<?php
class System_ListController extends Zend_Controller_Action {
    //Phuong thuc init()
    public function init() {
        //Load cau hinh thu muc trong file config.ini
        $app = Zend_Registry::get('App');
        //Lay duong dan thu muc goc (path directory root)
        $this->view->baseUrl = $baseUrl = $this->_request->getBaseUrl() . "/public/";
        $result = array(
                    array($baseUrl.'lib-js/system/admin/System_Admin_List.js','js')
                );                  
        if ($this->_request->isXmlHttpRequest()){
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'partial'
            ));
            $this->view->arrJsCss = Zend_Json::encode($result);
        }else{
            //Cau hinh cho Zend_layout
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'index'
            ));
            $response = $this->getResponse();
            //Dinh nghia current modul code
            $this->view->currentModulCode = "SYSTEM";
            $this->view->currentModulCodeForLeft = "list";
            $this->view->postHeader = Clound_String::registerFile($result);
            //Hien thi file template
            $response->insert('menu', $this->view->renderLayout('menu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('submenu', $this->view->renderLayout('submenu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('footer', $this->view->renderLayout('footer.phtml', './application/layout/scripts/'));   //Hien thi f
        }

    }

    public function indexAction() {
        $model = new system_models_Listtype();
        $aListtype = $model->getlist();
        $iTotal = sizeof($aListtype);
        $filterlisttype = array();
        for ($i=0; $i < $iTotal; $i++) { 
            array_push($filterlisttype, '<span style=display:none>##'.$aListtype[$i]['ListtypeID'].'##</span>'.$aListtype[$i]['Name']);
        }
        $this->view->filterlisttype = Zend_Json::encode($filterlisttype);
       
    }

    public function dataAction(){
        $model = new system_models_List();
        $parames = $this->_request->getParams();
        $data = $model->getAllList($parames);
        // ListtypeName
        $iCount = sizeof($data);
        if ($iCount) {
            $oCacheFile = new Clound_CacheFile();
            $oFile = new Clound_File(null,false);
            $arrDocType = $oCacheFile->getList('DM04');
            for ($i=0; $i < $iCount; $i++) { 
                
            }
        }
        echo Zend_Json::encode($data);
        die;
        $this->_helper->getHelper('viewRenderer')->setNoRender();
    }

    public function addAction() {
        if (!$this->_request->isXmlHttpRequest()) {
            return $this->_helper->redirector('index');
        }
        $modListtype = new system_models_Listtype();
        $modList = new system_models_List();
        $ListtypeID = $this->_request->getParam('ListtypeID');
        $ListID = $this->_request->getParam('ListID');
        $this->view->addHelperPath('Clound/Xml/Helper/', 'Clound_Xml_Helper');
        //Gan gia tri mac dinh cho cac Input
        $model = $modList->selectRow($ListID,$ListtypeID);
        // Session đơn vị
        $arrUnit = array();
        $arrUnit[0]['C_CODE'] ='QLB';
        $arrUnit[0]['C_NAME'] ='Quận A';
        $arrUnit[1]['C_CODE'] ='QB';
        $arrUnit[1]['C_NAME'] ='Quận B';
        $arrUnit[2]['C_CODE'] ='QC';
        $arrUnit[2]['C_NAME'] ='Quận C';
        $arrUnit[3]['C_CODE'] ='QD';
        $arrUnit[3]['C_NAME'] ='Quận D';            
        $_SESSION['DON_VI'] = $arrUnit;

        $filexml = $modListtype->getFileXmL($model['ListtypeID']);
        if($filexml =='') $filexml ='doi_tuong_danh_muc.xml';
        // File xml mô tả form
        $filexml = 'xml/system/'.$filexml;
        $xmlObj = new Clound_Xml($filexml,$model);
        $formObj = $xmlObj->formfieldGenerate();        $this->view->form=$formObj;

    }

    /**
     * @see : Thuc hien viec xoa Mot hoac Nhieu Doi tuong Danh muc
     */
    public function deleteAction() {
        $model = new system_models_List();
        $sListIdList = $this->_request->getParam('ListIDs');
        /*$sListIdList = str_replace(",", "','",  $sListTypeIdList);
        $sListIdList = "'$sListIdList'";*/
        if ($model->deleteRows($sListIdList)) {
            echo 1;
        }
        die();
    }

    public function saveAction() {
        // Tao doi tuong modeListType
        $model = new system_models_List();
        //Lay cac tham so cua form
        $sCode = trim(htmlspecialchars($this->_request->getParam('Code')));
        $sName = trim(htmlspecialchars($this->_request->getParam('Name')));
        $iOrders = trim($this->_request->getParam('Orders'));
        $iListID = (int) $this->_request->getParam('ListID','0');
        $iListtypeID = (int) $this->_request->getParam('ListtypeID');
        $iStatus = $this->_request->getParam('Status');
        $sOwnerCodeList = $this->_request->getParam('OwnerCodeList');
        $sXmlData = $this->_request->getParam('XmlData');
        if($sOwnerCodeList)
            $sOwnerCodeList = implode(',',$sOwnerCodeList);
        else $sOwnerCodeList = '';
        $data = array(
            "ListID"=> $iListID,
            "ListtypeID"=> $iListtypeID,
            "Code"=> $sCode,
            "Name"=> $sName,
            "Orders"=> $iOrders,
            "Status"=> $iStatus,
            "OwnerCodeList"=> $sOwnerCodeList,
            "XmlData"=> $sXmlData,
        );
        // var_dump($data);die;
        $errors = array();
        if($model->isValid($data)){
            if ($iListID === 0) {
                $data['ListID']= null;
                $iListID = $model->insert($data);
            } else {
                $where="ListID ='$iListID'";
                $model->update($data,$where);
            }
            if($iOrders){
                $model->reOrder($iListID,$iListtypeID,$iOrders);
            }
        }else{
            array_push($errors,array('Code'=>'Đã tồn tại'));
        }
        // echo $model->getAdapter()->lastInsertId();
        echo Zend_Json::encode($errors);
        die;
        $this->_helper->getHelper('viewRenderer')->setNoRender();
    }

}

?>